//202102104015柴亚楠
package cn.edu.sdjzu.xg.kcsj.controller.autuority;

import cn.edu.sdjzu.xg.kcsj.domain.User;
import cn.edu.sdjzu.xg.kcsj.domain.authority.Actor;
import cn.edu.sdjzu.xg.kcsj.domain.authority.ActorAssRole;
import cn.edu.sdjzu.xg.kcsj.domain.authority.Role;
import cn.edu.sdjzu.xg.kcsj.service.ActorAssRoleService;
import cn.edu.sdjzu.xg.kcsj.service.UserService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import util.JSONUtil;
import util.TokenHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;
@WebServlet("/login.ctl")
public class LoginController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType ("application/json;charset=UTF-8");
        //将request body转换为JSON字串
        String user_json = JSONUtil.getJSON(request);
        //将JSON字串解析为User对象
        User userToLogin = JSON.parseObject(user_json, User.class);

        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        try {
            //根据user对象获取actor对象
            Actor currentActor = UserService.getInstance().login(userToLogin);
            //没有获取到actor对象则向前端相应:用户名或密码不匹配
            if (currentActor == null) {
                message.put("message", "no have user!");
                response.getWriter().println(message);
            } else {
                //获得某Actor对象关联的所有ActorAssRole对象
                Collection<ActorAssRole> actorAssRoleCollection =
                        ActorAssRoleService.getInstance().findByActorId(currentActor);
                System.out.println(currentActor);
                //根据ActorAssRole对象获得对应的Role对象
                Collection<Role> roles = new TreeSet<>();
                for (ActorAssRole actorAssRole : actorAssRoleCollection) {
                    Role role = actorAssRole.getRole();
                    roles.add(role);
                }
                //根据username生成token令牌
                String token = TokenHelper.getToken(userToLogin.getUsername());
                request.getSession().setAttribute("token",token);
                message.put("actor", currentActor);
                message.put("roles", roles);
                message.put("token",token);
                //输出JSON字串时，屏蔽某些字段
                SimplePropertyPreFilter filter = new SimplePropertyPreFilter();
                filter.getExcludes().add("title");
                response.getWriter().println(JSON.toJSONString(message, filter));
            }
        } catch (SQLException e) {
            message.put("message", "sjk have exception");
            //响应message到前端
            response.getWriter().println(message);
            e.printStackTrace();
        } catch (Exception e) {
            message.put("message", "wl have exception");
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }
}
